class Router {
    constructor() {
        this.reset();
        this.monitorHash();
    }
    // 侦听hash变化
    monitorHash() {
        let hashchange = event => {
            let hash = location.hash.slice(1);
            if (!hash) return;
            if (this.isRoute(hash) === false && this.indexHash) hash = this.indexHash;
            this.load(hash, event);
        }
        window.addEventListener("hashchange", hashchange);
        window.addEventListener("DOMContentLoaded", hashchange);
    }
    // 注册页面逻辑
    route(hash, callback) {
        this.routers[hash] = callback || function () {
            throw new Error("请通过route方法添加" + hash + '执行逻辑');
        };
        return this;
    }
    // 是否注册了页面逻辑
    isRoute(hash) {
        if (this.routers[hash] === undefined) {
            this.route(hash);
            return false;
        }
        return true;
    }
    // 设置首页
    setIndex(hash) {
        this.indexHash = hash;
        this.isRoute(hash);
        return this;
    }
    // 重置路由配置
    reset() {
        this.routers = {};
        this.preHash = '';
        this.curHash = '';
        this.indexHash = '';
        return this;
    }
    // 跳转页面
    go(value) {
        if (typeof value === 'number') {
            history.go(value);
            return this;
        };
        if (typeof value === 'string') {
            location.hash = value;
            return this;
        };
        throw new Error("只支持数字和字符串的跳转标识");
        return this;
    }
    // 载入页面
    load(hash, e) {
        if (e.type == 'hashchange' && e.oldURL) {
            this.preHash = e.oldURL.match(/\#.*/)[0].slice(1);
        };
        this.curHash = hash;
        this.routers[hash](hash, this.preHash);
    }
}